package com.loto.insurance.mis.web.controller.ajax;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.loto.insurance.mis.web.authority.INSAuthority;
import com.loto.insurance.mis.web.entity.ajax.BaseJson;
import com.loto.insurance.mis.web.entity.ajax.GridJson;
import com.loto.insurance.mis.web.entity.ajax.CustInfo;
import com.loto.insurance.mis.web.enumeration.CustFromEnum;
import com.loto.insurance.mis.web.enumeration.CustStatusEnum;
import com.loto.insurance.mis.web.enumeration.CustTypeEnum;
import com.loto.insurance.mis.web.enumeration.MenuEnum;
import com.loto.insurance.mis.web.enumeration.ResultTypeEnum;
import com.loto.insurance.mis.web.service.CustInfoService;
import com.loto.insurance.mis.web.service.ProductInfoService;
import com.loto.insurance.mis.web.entity.Pager;
import com.loto.insurance.mis.web.entity.CustExtInfo;
import com.loto.utils.DateUtils;
import com.loto.utils.MD5;

@Controller
@SessionAttributes("ins_user")
public class CustJsonController {

  @Autowired
  CustInfoService custInfoService;

  @Autowired
  ProductInfoService productInfoService;

  @INSAuthority(authorityMenu = MenuEnum.CUSTOMER_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/customer/list.json", method = RequestMethod.POST)
  public ModelAndView customerlist(
	  @RequestParam(value = "customerName", required = false) String customerName,
	  @RequestParam(value = "customerBegin", required = false) String customerBegin,
	  @RequestParam(value = "customerEnd", required = false) String customerEnd,
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  @RequestParam(value = "rows", required = false, defaultValue = "20") int rows) {
	Map<String, Object> params = new HashMap<String, Object>();
	if (customerName != null && !customerName.isEmpty())
	  params.put("custName", customerName);
	if (customerBegin != null && customerEnd != null
	    && DateUtils.isDate(customerBegin, "yyyy-MM-dd")
	    && DateUtils.isDate(customerEnd, "yyyy-MM-dd")) {
	  params.put("custBegin", customerBegin);
	  params.put("custEnd", customerEnd);
	}

	Pager pager = new Pager();
	pager.setPageSize(rows);

	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	List<com.loto.insurance.mis.web.entity.CustInfo> customerList = custInfoService
	    .findAllCust(params);

	List<CustInfo> list = new ArrayList<CustInfo>();
	for (com.loto.insurance.mis.web.entity.CustInfo customer : customerList) {
	  CustInfo c = new CustInfo();
	  c.setCustId(customer.getCustId());
	  c.setCustName(customer.getCustName());
	  c.setCustUsefulFee(customer.getCustUsefulFee());
	  c.setCustCreatedOn(DateUtils.formatShortDateTime(customer
		  .getCustCreatedOn()));
	  c.setLevelName(customer.getLevelName());

	  int custFrom = customer.getCustFrom();
	  for (CustFromEnum e : CustFromEnum.values()) {
		if (e.getIndex() == custFrom) {
		  c.setCustFrom(e.getName());
		  break;
		}
	  }

	  int custStatus = customer.getCustStatus();
	  for (CustStatusEnum e : CustStatusEnum.values()) {
		if (e.getIndex() == custStatus) {
		  c.setCustStatus(e.getName());
		  break;
		}
	  }

	  int custType = customer.getCustTypeFid();
	  for (CustTypeEnum e : CustTypeEnum.values()) {
		if (e.getIndex() == custType) {
		  c.setCustType(e.getName());
		  break;
		}
	  }

	  list.add(c);
	}

	pager.setRowCount(custInfoService.findAllCustCount(params));

	ModelAndView mav = new ModelAndView();

	GridJson<CustInfo> gridJson = new GridJson<CustInfo>();
	gridJson.setTotal(pager.getRowCount());
	gridJson.setRows(list);

	mav.addObject("result", gridJson);
	return mav;
  }

  @INSAuthority(authorityMenu = MenuEnum.CUSTOMER_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/customer/add.json", method = RequestMethod.POST)
  public ModelAndView customeradd(
	  @RequestParam(value = "name", required = true) String custName,
	  @RequestParam(value = "pwd", required = true) String custPwd,
	  @RequestParam(value = "email", required = true) String custEmail,
	  @RequestParam(value = "from", required = true) int custFrom,
	  @RequestParam(value = "type", required = true) int custType,
	  @RequestParam(value = "level", required = true) int custLevel,
	  @RequestParam(value = "status", required = true) int custStatus,
	  @RequestParam(value = "contactName", required = true) String extContactName,
	  @RequestParam(value = "contactTel", required = true) String extContactTel,
	  @RequestParam(value = "contactOthers", required = true) String extContactOthers,
	  @RequestParam(value = "companyName", required = false) String extCompanyName,
	  @RequestParam(value = "companyZip", required = false) String extCompanyZip,
	  @RequestParam(value = "companyAddress", required = false) String extCompanyAddress,
	  @RequestParam(value = "companyTel", required = false) String extCompanyTel,
	  @RequestParam(value = "companyFax", required = false) String extCompanyFax,
	  @RequestParam(value = "companyEmail", required = false) String extCompanyEmail,
	  @RequestParam(value = "companyWebsite", required = false) String extCompanyWebsite,
	  @RequestParam(value = "companyReg", required = false) String extCompanyReg,
	  @RequestParam(value = "chargeName", required = false) String extChargeName,
	  @RequestParam(value = "extfrom", required = false) String extFrom) {

	com.loto.insurance.mis.web.entity.CustInfo custInfo = new com.loto.insurance.mis.web.entity.CustInfo();

	custInfo.setCustEmail(custEmail);
	custInfo.setCustName(custName);
	custInfo.setCustLevelFid(custLevel);
	custInfo.setCustPwd(MD5.GetMD5Code(custPwd));
	custInfo.setCustStatus(custStatus);
	custInfo.setCustFrom(custFrom);
	custInfo.setCustTypeFid(custType);

	CustExtInfo custExtInfo = new CustExtInfo();
	custExtInfo.setExtChargeName(extChargeName);
	custExtInfo.setExtCompanyAddress(extCompanyAddress);
	custExtInfo.setExtCompanyEmail(extCompanyEmail);
	custExtInfo.setExtCompanyFax(extCompanyFax);
	custExtInfo.setExtCompanyName(extCompanyName);
	custExtInfo.setExtCompanyReg(extCompanyReg);
	custExtInfo.setExtCompanyTel(extCompanyTel);
	custExtInfo.setExtCompanyWebsite(extCompanyWebsite);
	custExtInfo.setExtCompanyZip(extCompanyZip);
	custExtInfo.setExtContactName(extContactName);
	custExtInfo.setExtContactOthers(extContactOthers);
	custExtInfo.setExtContactTel(extContactTel);
	custExtInfo.setExtFrom(extFrom);

	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	base = custInfoService.addCustInfo(custInfo, custExtInfo);
	mav.addObject("result", base);
	return mav;
  }

  @INSAuthority(authorityMenu = MenuEnum.CUSTOMER_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/customer/modify.json", method = RequestMethod.POST)
  public ModelAndView customermodify(
	  @RequestParam(value = "id", required = true) long custId,
	  @RequestParam(value = "pwd", required = false) String custPwd,
	  @RequestParam(value = "email", required = true) String custEmail,
	  @RequestParam(value = "from", required = true) int custFrom,
	  @RequestParam(value = "type", required = true) int custType,
	  @RequestParam(value = "level", required = true) int custLevel,
	  @RequestParam(value = "status", required = true) int custStatus,
	  @RequestParam(value = "contactName", required = true) String extContactName,
	  @RequestParam(value = "contactTel", required = true) String extContactTel,
	  @RequestParam(value = "contactOthers", required = true) String extContactOthers,
	  @RequestParam(value = "companyName", required = false) String extCompanyName,
	  @RequestParam(value = "companyZip", required = false) String extCompanyZip,
	  @RequestParam(value = "companyAddress", required = false) String extCompanyAddress,
	  @RequestParam(value = "companyTel", required = false) String extCompanyTel,
	  @RequestParam(value = "companyFax", required = false) String extCompanyFax,
	  @RequestParam(value = "companyEmail", required = false) String extCompanyEmail,
	  @RequestParam(value = "companyWebsite", required = false) String extCompanyWebsite,
	  @RequestParam(value = "companyReg", required = false) String extCompanyReg,
	  @RequestParam(value = "chargeName", required = false) String extChargeName,
	  @RequestParam(value = "extfrom", required = false) String extFrom) {

	com.loto.insurance.mis.web.entity.CustInfo custInfo = new com.loto.insurance.mis.web.entity.CustInfo();
	custInfo.setCustId(custId);
	custInfo.setCustEmail(custEmail);
	custInfo.setCustLevelFid(custLevel);
	if (custPwd != null && !custPwd.isEmpty())
	  custInfo.setCustPwd(MD5.GetMD5Code(custPwd));
	custInfo.setCustStatus(custStatus);
	custInfo.setCustFrom(custFrom);
	custInfo.setCustTypeFid(custType);

	CustExtInfo custExtInfo = new CustExtInfo();
	custExtInfo.setExtCustFid(custId);
	custExtInfo.setExtChargeName(extChargeName);
	custExtInfo.setExtCompanyAddress(extCompanyAddress);
	custExtInfo.setExtCompanyEmail(extCompanyEmail);
	custExtInfo.setExtCompanyFax(extCompanyFax);
	custExtInfo.setExtCompanyName(extCompanyName);
	custExtInfo.setExtCompanyReg(extCompanyReg);
	custExtInfo.setExtCompanyTel(extCompanyTel);
	custExtInfo.setExtCompanyWebsite(extCompanyWebsite);
	custExtInfo.setExtCompanyZip(extCompanyZip);
	custExtInfo.setExtContactName(extContactName);
	custExtInfo.setExtContactOthers(extContactOthers);
	custExtInfo.setExtContactTel(extContactTel);
	custExtInfo.setExtFrom(extFrom);

	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	base = custInfoService.updateCustInfo(custInfo, custExtInfo);
	mav.addObject("result", base);
	return mav;
  }
}
